package geekstudy.week5;

import java.util.Arrays;

/**
 * https://leetcode-cn.com/problems/koko-eating-bananas/
 * 875. 爱吃香蕉的珂珂
 *
 *
 */
public class Week5KoKoEatingBananas {

    public int getHours(int[] piles, int speed) {
        int hours = 0;
        for(int i = 0; i < piles.length; i++) {
            hours += piles[i] / speed;
            if(piles[i] % speed > 0) {
                hours++;
            }
        }
        return hours;
    }

    public int minEatingSpeed(int[] piles, int h) {

        Arrays.sort(piles);
        int length = piles.length;
        int left = piles[0];
        int right = piles[length - 1];
        if(h == length) {
            return right;
        }

        while(left < right) {
            int middle = (left + right) / 2;
            int sum = 0;
            for(int i = 0; i < length; i++) {
                sum += piles[i] / middle;
                if(piles[i] % middle != 0) {
                    sum++;
                }
            }
            if(sum <= h) {
                right = middle;
            } else {
                left = middle + 1;
            }

        }
        return left;



        // int k = 0;
        //int minPileNumber = Integer.MAX_VALUE;
        //int maxPileNumber = Integer.MIN_VALUE;
        //for(int pile : piles) {
        //    if(pile < minPileNumber) {
        //        minPileNumber = pile;
        //    }
        //    if(pile > maxPileNumber) {
        //        maxPileNumber = pile;
        //    }
        //}

        //return k;
        //int sum = 0;
        //for(int pile : piles) {
        //    sum += pile;
        //}
        //if(sum % h == 0 ){
        //    k = sum / h;
        //} else {
        //    k = sum / h + 1;
        //}


    }

}
